const shutdowns = [];

function addItem(obj) {
    console.info(" shutdown add obj" + obj);
    shutdowns.push(obj);
}

//需要自关闭
function shutdown() {
    console.info(" shutdown in", shutdowns.length)
    let p = new Promise();
    let s = shutdowns.shift();
    if (s) {
        p.then(s).catch(s).finally(shutdown);
    } else {
        console.info("所有资源已释放");
        // 完成后退出进程
        process.exit(0);
    }
}

function lineShutdown() {
    process.on('beforeExit', shutdown);
}

// 例如，在接收到退出信号时
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
module.exports = { addItem, shutdown, lineShutdown }